<template>
  <transition name="slide">
    <!-- 因为他在playlist组件上所以会冒泡下去： -->
    <div class="add-song" v-show="showFlag" @click.stop>
      <div class="header">
        <h1 class="title">添加歌曲到列表</h1>
        <div class="close" @click="hide">
          <i class="icon-close iconfont iconshanchuanniu"></i>
        </div>
      </div>
      <div class="search-box-wrapper search">
        <van-search v-model="query" placeholder="搜索歌曲" background="transparent" autofocus />
      </div>
      <div class="shortcut" v-show="!query">
        <switches :switches="switches" :currentIndex="currentIndex" @switch="switchItem"></switches>
        <div class="list-wrapper">
          <!-- 显示第一个列表 -->
          <scroll
            v-if="currentIndex===0"
            :data="playHistory"
            class="list-scroll"
            ref="songList"
            :refreshDelay="100"
          >
            <div class="list-inner">
              <song-list :songs="playHistory" @select="selectSong"></song-list>
            </div>
          </scroll>
          <scroll class="list-scroll" v-if="currentIndex===1" ref="hisList" :refreshDelay="100">
            <div class="list-inner">
              <search-list
                :searches="searchHistory"
                @select="selectItem"
                @delete="deleteSearch"
                showRow
              ></search-list>
            </div>
          </scroll>
        </div>
      </div>
      <div class="search-result" v-show="query">
        <suggest :query="query" @select="onSaveSearch" />
      </div>
      <notify ref="notify">
        <div class="tip-title">
          <i class="iconfont iconduigou icon-ok"></i>
          <span class="text">歌曲添加到播放队列成功</span>
        </div>
      </notify>
    </div>
  </transition>
</template>

<script>
import { Search } from "vant";
import { mapActions, mapGetters } from "vuex";
import Suggest from "@/components/suggest/suggest";
import Switches from "@/base/switches/switches";
import Scroll from "@/base/scroll/scroll";
import SongList from "@/base/song-list/song-list-normal";
import Song from "@/common/song";
import SearchList from "@/base/search-list/search-list";
import Notify from "@/base/notify/notify";
export default {
  data() {
    return {
      showFlag: false,
      showSinger: false,
      currentIndex: 0,
      switches: [{ name: "最近播放" }, { name: "搜索历史" }],
      query: "",
      shownotify: false
    };
  },
  computed: {
    ...mapGetters(["playHistory", "searchHistory"])
  },
  methods: {
    switchItem(index) {
      this.currentIndex = index;
    },
    ...mapActions(["saveSearchHistory", "insertSong", "deleteSearchHistory"]),
    show() {
      this.showFlag = true;
      setTimeout(() => {
        if (this.currentIndex === 0) {
          this.$refs.songList.refresh();
        } else {
          this.$refs.hisList.refresh();
        }
      }, 20);
    },
    hide() {
      this.showFlag = false;
    },
    onSaveSearch(item) {
      this.saveSearchHistory(item);
      this.$refs.notify.show();
    },
    selectSong(song, index) {
      if (index !== 0) {
        this.insertSong(new Song(song));
        this.$refs.notify.show();
      }
    },
    selectItem(item) {
      console.log(typeof item);
      this.insertSong(new Song(item));
      this.$refs.notify.show();
    },
    deleteSearch(item) {
      this.deleteSearchHistory(item);
      this.$refs.hisList.refresh();
    }
  },
  components: {
    [Search.name]: Search,
    Suggest,
    Switches,
    Scroll,
    SongList,
    SearchList,
    Notify
  }
};
</script>

<style lang="less" scoped>
.add-song {
  position: fixed;
  top: 0;
  bottom: 0;
  width: 100%;
  z-index: 200;
  background: @color-background;
  .van-search {
    padding: 0;
  }
  &.slide-enter-active,
  &.slide-leave-active {
    transition: all 0.3s;
  }
  &.slide-enter,
  &.slide-leave-to {
    transform: translate3d(100%, 0, 0);
  }
  .header {
    position: relative;
    height: 44px;
    text-align: center;
    .title {
      line-height: 44px;
      font-size: @font-size-large;
      color: @color-text;
    }
    .close {
      position: absolute;
      top: 0;
      right: 8px;
      .icon-close {
        display: block;
        padding: 12px;
        font-size: 20px;
        color: @color-theme;
      }
    }
  }
  .search-box-wrapper {
    margin: 20px;
  }
  .shortcut {
    .list-wrapper {
      position: absolute;
      top: 180px;
      bottom: 0;
      width: 100%;
      .list-scroll {
        height: 100%;
        overflow: hidden;
        .list-inner {
          padding: 20px 20px;
          padding-top: 0;
        }
      }
    }
  }
  .search-result {
    position: fixed;
    top: 140px;
    bottom: 0;
    width: 100%;
  }
  .tip-title {
    text-align: center;
    padding: 18px 0;
    font-size: 0;

    .icon-ok {
      font-size: @font-size-medium;
      color: @color-theme;
      margin-right: 4px;
    }
    .text {
      font-size: @font-size-medium;
      color: @color-text;
    }
  }
}
</style>